package com.example.zero.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.zero.R;
import com.example.zero.Utils.ApplicationUtil;
import com.example.zero.Utils.SharedPreUtil;

public class WelcomeActivity extends AppCompatActivity {
    //private boolean flag = false;
    private Button btn_jump;


    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                //判断用户是否登录
                boolean userIsLogin = (boolean) SharedPreUtil.getParam(WelcomeActivity.this,
                        SharedPreUtil.IS_LOGIN, false);
                if (userIsLogin) {
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(WelcomeActivity.this, ZLoginActivity.class);
                    startActivity(intent);
                }

                finish();
            } else if (msg.what == 0) {
                thread.interrupt();
            }

        }

    };

    final Message message = new Message();
    final Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                message.what = 1;
                handler.sendMessage(message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        boolean isFirstOpen = SharedPreUtil.getBoolean(this, SharedPreUtil.FIRST_OPEN, true);
        if(isFirstOpen){
            Intent intent = new Intent(this, WelcomeGuideActivity.class);
            startActivity(intent);
            /*注意，需要使用finish将该activity进行销毁，否则，在按下手机返回键时，会返回至启动页*/
            finish();
            return;
        }
        thread.start();
        btn_jump = (Button) findViewById(R.id.btn_jump);
        btn_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                flag = true;
                message.what = 0;
                if(handler.obtainMessage(message.what, message.obj) != null){
                    Message _msg = new Message();
                    _msg.what = message.what;
                    _msg.obj= message.obj;
                    handler.sendMessage(_msg);
                }else{
                    handler.sendMessage(message);
                }


                //判断用户是否登录
                boolean userIsLogin = (boolean) SharedPreUtil.getParam(WelcomeActivity.this,
                        SharedPreUtil.IS_LOGIN, false);
                Log.e("WelcomeActivity","userIsLogin: "+userIsLogin);
                if (userIsLogin) {
                    Log.d("WelcomeActivity","userIsLogin: T");
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Log.d("WelcomeActivity","userIsLogin: F");
                    Intent intent = new Intent(WelcomeActivity.this, ZLoginActivity.class);
                    startActivity(intent);
                }

                finish();


            }
        });
        ApplicationUtil.getInstance().addActivity(this);
    }
}
